#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include "raycaster_view_dialog.h"

#define FPS 30.0

MainWindow::MainWindow(Scene *scene, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    scene_(scene)
{
    ui->setupUi(this);
    this->setWindowTitle("Renderizado com OpenGL");

    // Comment/Uncomment to disable/enable the renderization via raycasting
//    RayCasterViewDialog *rcDialog = new RayCasterViewDialog(scene, this);
//    rcDialog->setModal(true);
//    rcDialog->show();

    this->ui->opengl_widget->setScene(scene);
    QTimer *timer = new QTimer(this);
    timer->start(1/FPS);

    QObject::connect(timer, SIGNAL(timeout()), this->ui->opengl_widget, SLOT(updateGL()));
    QObject::connect(this->ui->opengl_widget, SIGNAL(asking_raycast()), this, SLOT(raycast()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::raycast()
{
    RayCasterViewDialog *rcDialog = new RayCasterViewDialog(scene_, this);
    rcDialog->setModal(true);
    rcDialog->show();
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
